using System.Data;
using System.Collections;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System;
using System.IO;
using System.Text;
using Microsoft.ReportingServices.ReportRendering;
using Microsoft.ReportingServices.Interfaces;
using System.Web;

namespace CustomExcelRenderCS
{
	internal class ImageRenderer
	{
        internal CreateAndRegisterStream m_createAndRegisterStreamCallback;
        protected bool m_htmlFragment;
        //protected StreamWriter m_textWriter;
        //System.IO.StreamWriter sw;
        string DestImageRoot;

        protected Microsoft.ReportingServices.ReportRendering.Report m_report;

		internal ImageRenderer()
		{
            m_report = Renderer.mReport;
            m_createAndRegisterStreamCallback = Renderer.mCreateAndRegisterStream;
            DestImageRoot = Renderer.mDeviceInfo.GetValues("StreamRoot")[0];
		}
		
		protected internal string Render(Microsoft.ReportingServices.ReportRendering.Image image)
		{
            int imageWidth = Convert.ToInt32(image.Width.ToPoints());
            int imageHeight = Convert.ToInt32(image.Height.ToPoints());
            //Image image = new Bitmap(imageWidth,imageHeight);
            string fileExt = image.MIMEType;
            fileExt = fileExt.Replace("image/", ".");

            if (!image.Name.Contains ("custom_image") )            
            {
                string StreamRoot = DestImageRoot;
                return "<img src=\"" + StreamRoot + image.Name + fileExt + "\" height=\"" + imageHeight + "\" width = \"" + imageWidth + "\" />";
            }
            else {
                string imageSrc = RenderHTMLImageUrl(image, image.UniqueName, "");
                return "<img src=\"" + imageSrc + fileExt + "\" height=\"" + imageHeight + "\" width = \"" + imageWidth + "\" />";
            }
		}

        private string RenderHTMLImageUrl(IImage image, string uniqueName, string streamNamePrefix)
        {
            string streamName = image.StreamName;
            string imageRoot = Renderer.srvPath;
            string imageUrl = "";

            if (streamName == null)
            {
                byte[] imageData = image.ImageData;
                if (imageData != null)
                {
                    string name = this.GenerateImageStreamName(uniqueName, streamNamePrefix);
                    this.m_createAndRegisterStreamCallback(name, string.Empty, null, image.MIMEType, false, StreamOper.CreateAndRegister).Write(imageData, 0, imageData.Length);
                    //if (this.m_processAction == ProcessAction.HTMLStyles)
                    //{
                       return this.EncodeCSSStyle(this.m_report.StreamURL(this.IsFragment, name));
                    //}
                    //else
                    //{
                    //    this.WriteAttrEncoded(this.m_report.StreamURL(this.IsFragment, name));
                    //}
                }
            }
            else
            {
                imageUrl = DestImageRoot + streamName;     //Renderer.mReport.Name;   // + "&rs:ImageID=";

                //imageUrl = imageRoot + imageUrl + streamName;
            }


            return imageUrl;
        }

        private string GenerateImageStreamName(string uniqueName, string streamNamePrefix)
        {
            StringBuilder builder = new StringBuilder(streamNamePrefix);
            builder.Append('_');
            builder.Append(uniqueName);
            //if ((this.m_reportSection == ReportSection.PageHeader) || (this.m_reportSection == ReportSection.PageFooter))
            //{
            //    builder.Append('_');
            //    builder.Append(this.m_pageStart);
            //    builder.Append('_');
            //    builder.Append((this.m_reportSection == ReportSection.PageHeader) ? "H" : "F");
            //}
            return builder.ToString();
        }

        protected string EncodeCSSStyle(string input)
        {
            if (input != null)
            {
                StringBuilder builder = new StringBuilder(input);
                for (int i = 0; i < builder.Length; i++)
                {
                    if (((builder[i] == '\\') || (builder[i] == '\'')) || (((builder[i] == '(') || (builder[i] == ')')) || (builder[i] == ',')))
                    {
                        builder.Insert(i, '\\');
                        i++;
                    }
                }
                return builder.ToString(); 

                //this.WriteStream(builder.ToString());
            }
            return String.Empty;
        }

        protected string WriteAttrEncoded(string theString)
        {
            return HttpUtility.HtmlAttributeEncode(theString);
        }



        // Properties
        internal bool IsFragment
        {
            get
            {
                return true;             //(this.m_htmlFragment && (null == this.m_actionScript));
            }
        }

        //protected void WriteStream(string theString)
        //{
        //    if (!string.IsNullOrEmpty(theString))
        //    {
        //        //if (this.m_isMHTML)
        //        //{
        //        //    int length = theString.Length;
        //        //    if ((this.m_outputLineLength + length) < 950)
        //        //    {
        //        //        this.m_textWriter.Write(theString);
        //        //        this.m_outputLineLength += length;
        //        //    }
        //        //    else
        //        //    {
        //        //        int num4;
        //        //        this.WriteStreamLineBreak();
        //        //        for (int i = 0; i < length; i += num4)
        //        //        {
        //        //            int num3 = length - i;
        //        //            if (num3 <= 950)
        //        //            {
        //        //                this.m_textWriter.Write(theString.Substring(i, num3));
        //        //                this.m_outputLineLength = num3;
        //        //                break;
        //        //            }
        //        //            num4 = 950;
        //        //            while ((num4 > 0) && !char.IsWhiteSpace(theString[i + num4]))
        //        //            {
        //        //                num4--;
        //        //            }
        //        //            if (num4 == 0)
        //        //            {
        //        //                num4 = 950;
        //        //            }
        //        //            this.m_textWriter.WriteLine(theString.Substring(i, num4));
        //        //        }
        //        //    }
        //        //}
        //        //else
        //        //{
        //            this.m_textWriter.Write(theString);
        //            //if (this.m_copyStreamToBuffer)
        //            //{
        //            //    this.m_streamBuffer.Append(theString);
        //            //}
        //        //}
        //        //this.m_needFlush = true;
        //    }
        //}




	}


}
